
<template>
  <div class="tempxyz menmuStyle">
    <CCard class="rightCard">
      <CCardHeader class="header-wraper">
        <div class="pushtaskHeadStyle">{{$route.query.data && $route.query.data.flowId?"更新推送任务":"创建推送任务"}}</div>
        <div>
          <Button type="primary"  style="margin-right: 4px" @click="quxiao"
          >取消</Button
          >
          <Button type="primary" @click="confime" >确定</Button>
        </div>
      </CCardHeader>

      <CCardBody class="card-body">
        <Split v-model="split1">
          <div slot="left" class="demo-split-pane">
            <Form :model="taskParameter" :label-width="90">
              <FormItem label="任务名称">
                <Input v-model="taskParameter.taskName"></Input>
              </FormItem>
              <FormItem label="任务描述" >
                <Input v-model="taskParameter.taskDetail" show-word-limit type="textarea" maxlength="100" placeholder="最多100字 " style="margin-bottom:10px"></Input>
              </FormItem>

              <FormItem label="创 建 人">
                <Select v-model="taskParameter.taskOwner">
                  <Option
                      v-for="item in renyuan"
                      :value="item.username"
                      :key="item.username"
                  >{{ item.username }}</Option
                  >
                </Select>
              </FormItem>
              <FormItem label="优 先 级">
                <RadioGroup v-model="taskParameter.priorityLevel">
                  <Radio
                      v-for="opt in options"
                      :label="opt.value"
                      :key="opt.value"
                  >{{ opt.label }}</Radio
                  >
                </RadioGroup>
              </FormItem>


              <FormItem label="扫描目录">
               <div style="display: flex">
                 <Input
                     v-model="taskParameter.scanDir"
                     placeholder="请选择目录"
                 ></Input>
                 <Button
                     type="primary"
                     @click="selectPushTaskProDir('saoMiao')">...
                 </Button>
               </div>
              </FormItem>
              <FormItem label="生产目录" >
                <div style="display: flex">
                  <Input
                      v-model="taskParameter.proDir"
                      placeholder="请选择目录"
                  ></Input>
                  <Button
                      type="primary"
                      @click="selectPushTaskProDir('sengChan')"
                  >...</Button
                  >
                </div>
              </FormItem>
              <FormItem label="扫描间隔">
                <div style="display: flex">
                  <Select v-model="taskParameter.scanType"  style="width: 50%">
                    <Option
                        v-for="item in timerList"
                        :value="item.value"
                        :key="item.value">
                      {{ item.label }}
                    </Option>
                  </Select>
                  <InputNumber
                      style="width: 50%"
                      v-model="taskParameter.scanInterval"
                      :min="jianGeFanWei.min"
                      :max="jianGeFanWei.max"
                  >
                  </InputNumber>
                </div>
              </FormItem>
              <FormItem label="数据后缀" >
                <Input v-model="taskParameter.suffixFlag" placeholder="以英文符号 ; 分割"></Input>
              </FormItem>

              <FormItem label="完整标识" >
                <Input
                    v-model="taskParameter.finishFlag"
                    placeholder="如果设置标识，有标识才能触发任务">
                </Input>
              </FormItem>

              <FormItem label="处理规则" >
                <Select
                    v-model="taskParameter.selectData"
                    style="width: 50%"
                    :transfer="true"
                    :placeholder="
                            taskParameter.selectData == 'ABC'
                              ? '以^开头,以$结尾'
                              : '请输入内容'
                          "
                >
                  <Option
                      v-for="item in dataRuleSelection"
                      :value="item.value"
                      :key="item.value">{{ item.label }}</Option>
                </Select>
                <Input      v-model="taskParameter.regexExpressData"  :placeholder="taskParameter.selectData == 'ABC'  ? '以^开头,以$结尾'  : '请输入内容'" style="width: 50%"></Input>
              </FormItem>
            </Form>
          </div>
          <div slot="right" class="demo-split-pane">
            <div style="display: flex">
              <div style="flex: 1;padding: 0 10px ">
                  <Select
                      style="margin-bottom: 10px"
                      v-model="taskParameter.flowId"
                      filterable
                      transfer
                      @on-select="processingOnChange"
                      placeholder="请输入关键字过滤"
                  >
                    <Option
                        v-for="item in processingList"
                        :value="item.id"
                        :key="item.id"
                    >{{
                        item.workflowName +
                        "-" +
                        item.workflowVersion +
                        ".0"
                      }}</Option
                    >
                  </Select>
                  <div style="height: calc(100vh - 245px);overflow-y: auto">
                    <tuiSongCanShu :workflowId="taskParameter.flowId"  ref="canShu" v-show="taskParameter.flowId" ></tuiSongCanShu>
                  </div>
              </div>
              <div style="display: flex; width: 320px;margin-left: 10px">
                <imgBox :workflowId="taskParameter.flowId"></imgBox>
              </div>
            </div>
          </div>
        </Split>
      </CCardBody>
    </CCard>




    <FileCatalog
        ref="refFileCatalogModal"
        v-if="fileCatalogModal"
        :modalShow="fileCatalogModal"
        @closed="fileCatalogModalClose"
        @menuOk="fileCatalogModalOk"
    ></FileCatalog>


<!--    <Modal
      v-model="showPushTaskZtreeModal"
      draggable
      scrollable
      title="推送任务选择目录"
      id="my-modal"
    >
      <p slot="header" class="modalHeader">选择目录</p>
      <div class="treeDiv">
        <ul id="pushTaskTree" class="ztree"></ul>
      </div>
      <template #footer>
        <Button @click="pushTaskMenuCancel" color="info" class="cancel-btn">取消</Button>
        <Button @click="pushTaskMenuOk" color="info" class="system-btn-style">确定</Button>
      </template>
    </Modal>-->


  </div>
</template>

<script>

import FileCatalog from "../jobProcess/fileCatalog.vue";
import imgBox from "../../template/taskList/imgBox";
import tuiSongCanShu from "../../template/taskList/tuiSongCanShu";
export default {
  name: "updateAddtask",
  components: {
    FileCatalog,
    tuiSongCanShu,
    imgBox
  },
  data() {
    return {
      taskParameter: {
        taskOwner: window.sessionStorage.getItem("user"),
        priorityLevel: 0, //优先级
        proDir: "",
        scanDir: "",
        scanType: undefined,
        scanInterval: 1,
        suffixFlag: "",
        finishFlag: "",

        taskName: "",
        taskDetail:"",
        flowName: "",
        flowId: "", //就是流程id
        regexExpressData: "",
        selectData: ""
        //"department":"zyh" 、部门、 暂无
      },

      renyuan: [], //创建人下拉
      options: [
        { label: "普通", value: 0 },
        { label: "优先", value: 1 },
        // { label: "应急", value: "2" },//后续需要再加上
      ],  // 优先级选择
      timerList: [
        {
          label: "按分钟",
          value: 1,
        },
        {
          label: "按小时",
          value: 2,
        },
        {
          label: "按天",
          value: 3,
        },
        {
          label: "按月",
          value: 4,
        },
      ], // 扫描间隔
      dataRuleSelection: [
        { label: "文件开头", value: "^ABC.*$" },
        { label: "文件结尾", value: "^.*ABC$" },
        { label: "文件包含", value: "^.*ABC.*$" },
        { label: "文件不包含", value: "^((?!ABC).)*$" },
        /*    {label:"不包含ABC",value:"^((?!ABC).)*str((?!ABC).)*$"},*/
        { label: "自定义", value: "ABC" },
      ], //文件规则
      processingList: [], // 流程下拉


      fileCatalogModal: false,
      split1: 0.22,
      userName: "",
      isSaoMiao: true,


      jianGeFanWei:{
        min: 0,
        max:59
      }

    };
  },
  watch: {
   "taskParameter.scanType":{
     handler(newData) {
       if (newData===1) {   this.jianGeFanWei={ min: 0, max:59}  }
       if (newData===2) {   this.jianGeFanWei={ min: 0, max:23}  }
       if (newData===3) {   this.jianGeFanWei={ min: 1, max:31}  }
       if (newData===4) {   this.jianGeFanWei={ min: 1, max:12}  }
     }
   }

  },
  mounted() {
    this.getProcessingData();
    this.huoQuYongHu();

    this.userName = window.localStorage.getItem("user");
    if (this.$route.query.data){
      this.taskParameter = JSON.parse(this.$route.query.data.taskParameter);
    }
  },
  methods: {
    getProcessingData() {
      let that = this;
      let path = this.$api.processingApi.search;
      this.http.get(path).then((res) => {
        if (res.data.code == 0) {
          that.processingList = res.data.data;
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },
    processingOnChange(item) {
      this.taskParameter.flowId = item.value;
      this.taskParameter.flowName = item.label;
    },

    // 应急推送任务生产目录选择
    selectPushTaskProDir(type) {
      let that = this;
      that.isSaoMiao=type
      this.fileCatalogModal = true;  // 打开弹框
      this.$nextTick(() => {
        that.$refs.refFileCatalogModal.initFileTree({ type:{name:'dir'} }, false);    //控制文件文件夹   是否多选
      });
    },

    fileCatalogModalOk(data) {
      if (this.isSaoMiao === "saoMiao") {
        this.taskParameter.scanDir = data;
      } else {
        this.taskParameter.proDir = data;
      }
      this.fileCatalogModal = false;
    },
    fileCatalogModalClose() {
      this.fileCatalogModal = false;
    },

    huoQuYongHu(){
      let that = this;
      this.http.get(this.$api.manageApi.queryAllUser).then((res) => {
        that.renyuan = res.data.data.userList;
      });
    },
/*
    workflowData(data){
      this.workflowData = data;
    },*/
    confime() {
      let data = this.$refs.canShu.getWorkflowData();
      let paramsData = this.detalCanshu(data);
      let isUpload=( this.$route.query.data&&this.$route.query.data.flowId)
      this.taskParameter.regexExpress =this.taskParameter.regexExpressData? this.taskParameter.selectData.replace("ABC", this.taskParameter.regexExpressData):"";
      let path =isUpload? this.$api.processingApi.updateTuiSong+this.$route.query.data.id : this.$api.processingApi.create;
      this.http[isUpload?"put":"post"](path, {
        taskParameter:JSON.stringify(this.taskParameter),
        flowKey:JSON.stringify(paramsData),
        taskName: this.taskParameter.taskName,
        taskDetail: this.taskParameter.taskDetail,
        sourceId:2,
        flowId:this.taskParameter.flowId,
        flowName:this.taskParameter.flowName,
        status: 0,
      }).then((res) => {
        this.$Message.success("操作成功!")
        if (res.data.success){
          this.$router.go(-1);
        }
      })
    },
    // 处理参数
    detalCanshu(data){
      let params = {};
      let dataList = JSON.parse(data.item);
      this.taskParameter.skipTaskReferenceNames = data.skipTaskReferenceNames;
      this.taskParameter.runtime = data.runtime;
      if (dataList.globalParam && dataList.globalParam.length) {
        dataList.globalParam.forEach((item)=>{
          params[item.workflowInputKey] = item.tuisong?item.tuisong:item.default;
        });
      }
      if (dataList.inputParam&&dataList.inputParam.length) {
        dataList.inputParam.forEach((item)=>{
          item.forEach((list)=>{
            params[list.workflowInputKey] = list.tuisong?list.tuisong:list.default;
          })
        });
      }

      return params;

    },
    quxiao(){
      this.$router.go(-1);
    }


  },



};
</script>

<style scoped>
.pushtaskHeadStyle {
  font-size: 24px;
  font-weight: 500;
  color: rgb(19, 17, 17);
}
.demo-split-pane {
  padding: 10px;
}
/deep/.ivu-form-item {
  margin-bottom: 0;

}

/deep/.ivu-checkbox-wrapper{
  width: 108px;
}
</style>